home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Random2.0 / Source / TestGaussian.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  176 lines

  1. //
  2. // BinTest
  3. //
  4. // This program tests the Random classes' abilities to generate
  5. // random gaussian variables.
  6. //
  7.  
  8.  
  9. #import "Gaussian.h"
  10. #import "StandardEngine.h"
  11. #import "ElkinsEngine.h"
  12. #import "R250Engine.h"
  13. #import <math.h>
  14. #import <stdio.h>
  15. #import <stdlib.h>
  16.  
  17.  
  18. #define DEFAULT_N    1000
  19. #define DEFAULT_M    10
  20. #define RANGE        3.5
  21.  
  22. #define MAX_BAR        40
  23.  
  24.  
  25. //
  26. // test_gaussian()
  27. //
  28.  
  29. int test_gaussian(id myRand, int m, int n)
  30. {
  31.     int            max = 0;        // Size of largest bin.
  32.     int            i;            // Generic loop variable.
  33.     int            j;            // Generic loop variable.
  34.     double        x;            // The random number.
  35.     int            slot;            // The sorted position of a number.
  36.     int            barsize;        // The length of a result bar.
  37.     int            *binneg;        // The bin array (dynamically allocated).
  38.     int            *binpos;        // The bin array (dynamically allocated).
  39.     int            which;
  40.     int            total = 0;
  41.     
  42.     //
  43.     // Allocate and initialize slot array:
  44.     //
  45.     
  46.     binneg = (int *)malloc(m * sizeof(int));
  47.     binpos = (int *)malloc(m * sizeof(int));
  48.     for(i = 0; i < m; i++) {
  49.         binneg[i] = 0;
  50.     binpos[i] = 0;
  51.     }
  52.     
  53.     //
  54.     // Make n random numbers:
  55.     //
  56.         
  57.     for(i = 0; i < n; i++) {
  58.         do {
  59.         x = [myRand gaussian];
  60. //        printf("Made gaussian %f\n", x);
  61.     } while (x == 0.0);
  62.     
  63.     total++;
  64.     which = (x > 0);
  65.     slot = (int)floor(fabs(x) * m / RANGE);
  66.     if((slot >= 0) && (slot < m) && (x != 0.0)) {        // If it is in range,
  67.         switch(which) {                    //   count it.
  68.             case 0:
  69.             if((++binneg[slot]) > max) {
  70.             max = binneg[slot];
  71.             }
  72.             break;
  73.         case 1:
  74.             if((++binpos[slot]) > max) {
  75.             max = binpos[slot];
  76.             }
  77.             break;
  78.         default:
  79.             printf("Why is which = %d?\n", which);
  80.             break;
  81.         }
  82.     }
  83.     }
  84.     
  85.     //
  86.     // Print the results:
  87.     //
  88.     
  89.     for(i = m - 1; i >= 0; i--) {
  90.         printf("%+1.6f to %+1.6f: %6d:", -((i + 1) * (RANGE / m)),
  91.         -(i * (RANGE / m)), binneg[i]);
  92.     
  93.     barsize = (int)((double)binneg[i] / ((double)max / (double)MAX_BAR));
  94.     for(j = 0; j < barsize; j++)
  95.         printf("*");
  96.     printf("\n");
  97.     }
  98.     for(i = 0; i < m; i++) {
  99.         printf("%+1.6f to %+1.6f: %6d:", i * (RANGE / m), (i + 1) * (RANGE / m), binpos[i]);
  100.     
  101.     barsize = (int)((double)binpos[i] / ((double)max / (double)MAX_BAR));
  102.     for(j = 0; j < barsize; j++)
  103.         printf("*");
  104.     printf("\n");
  105.     }
  106.     
  107.     printf("Max   = %6d\n", max);
  108.     printf("Total = %6d\n", total);
  109.     
  110.     //
  111.     // Clean up:
  112.     //
  113.     
  114.     free(binneg);
  115.     free(binpos);
  116.     
  117.     //
  118.     // Return to caller:
  119.     //
  120.     
  121.     return 0;
  122. }
  123.  
  124.  
  125. //
  126. // main()
  127. //
  128.  
  129. int main(int argc, char *argv[])
  130. {
  131.     id            myRand;            // Random number generator.
  132.     int            n;            // Quantity of numbers to generate.
  133.     int            m;            // Number of bins in which to sort them.
  134.     
  135.     myRand = [[Gaussian alloc] initEngineClass:[ElkinsEngine class]];
  136.     
  137.     switch(argc) {
  138.         case 2:
  139.         sscanf(argv[1], "%d", &n);
  140.         m = DEFAULT_M;
  141.         break;
  142.     case 3:
  143.         sscanf(argv[1], "%d", &n);
  144.         sscanf(argv[2], "%d", &m);
  145.         break;
  146.     default:
  147.         n = DEFAULT_N;
  148.         m = DEFAULT_M;
  149.         break;
  150.     }
  151.     
  152.     printf("TestGaussian: Testing StandardEngine class:\n");
  153.     myRand = [[Gaussian alloc] initEngineClass:[StandardEngine class]];
  154.     test_gaussian(myRand, m, n);
  155.     printf("\n\n");
  156.     [myRand free];
  157.     
  158.     printf("TestGaussian: Testing ElkinsEngine class:\n");
  159.     myRand = [[Gaussian alloc] initEngineClass:[ElkinsEngine class]];
  160.     test_gaussian(myRand, m, n);
  161.     printf("\n\n");
  162.     [myRand free];
  163.     
  164.     printf("TestGaussian: Testing R250Engine class:\n");
  165.     myRand = [[Gaussian alloc] initEngineClass:[R250Engine class]];
  166.     test_gaussian(myRand, m, n);
  167.     printf("\n\n");
  168.     [myRand free];
  169.     
  170.     return 0;
  171. }
  172.  
  173.  
  174. //
  175. // End of file.
  176. //